<?php

namespace App\Http\ControllersVer1;

use Illuminate\Http\Request;

//use Requests;
use Uuid;

use App\Book;
use App\BookImage;
use App\Appointment;
use App\Letter;
use App\LetterPhoto;

//use Illuminate\Support\Facades\App;

class AppointmentController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //


    public function test() {


        return response()->json([
            'code' => 200,
            'msg' => 'api v1 test success!'
        ]);

    }





    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function handleAppointment(Request $request) {

        $this->validate($request, [
            'action' => 'required',
            'book_uuid' => 'required'
        ]);

        $action = $request->input('action');
        $book_uuid = $request->input('book_uuid');

        $book = Book::where('uuid', $book_uuid)->first();

        if (is_null($book)) {

            return response()->json([
                'code' => 4060,
                'msg' => 'wrong book uuid!'
            ]);

        }

        $me = $request->user();

        //current ongoing appointment
        $ongoing_appointment = Appointment::where('book_id', $book->id)
            ->whereIn('status', ['os', 'ap'])
            ->first();

        switch ($action) {

            case "os":
                //os: open new appointment by successor;

                if ($book->holder_id == $me->id) {
                    return response()->json([
                        'code' => 4060,
                        'msg' => 'you are the book holder.'
                    ]);
                }

                if (is_null($ongoing_appointment)) {
                    $stones = 2;

                } else {

                    if ($ongoing_appointment->successor_id == $me->id) {
                        return response()->json([
                            'code' => 4060,
                            'msg' => 'I am the successor of the ongoing appointment.'
                        ]);
                    }

                    if ($ongoing_appointment->status == 'os') {
                        $stones = $ongoing_appointment->stones + 1;

                    } else {
                        return response()->json([
                            'code' => 4060,
                            'msg' => 'ongoing appointment status not os.'
                        ]);
                    }
                }

                //todo: check if the user has enough stones to pay.


                if (!is_null($ongoing_appointment)) {
                    //ro: replaced by other user.
                    $ongoing_appointment->status = "ro";
                    $ongoing_appointment->save();
                    //$ongoing_appointment->delete();

                    //todo give back the previous_appointment successor's stones.
                    //todo: send sms message to the previous_appointment successor.
                }

                //
                $new_appointment = new Appointment;
                $new_appointment->book_id = $book->id;
                $new_appointment->predecessor_id = $book->holder_id;
                $new_appointment->successor_id = $me->id;
                $new_appointment->stones = $stones;
                $new_appointment->status = "os";
                $new_appointment->save();

                //todo: record the the stones of the new successor.
                //todo: send sms message to the book holder
                //to sent a letter
                $letter = new Letter;
                $letter->uuid = str_replace('-', '', Uuid::generate(4)->string);
                $letter->sender_id = $me->id;
                $letter->receiver_id = $book->holder->id;
                $letter->save();

                //?
                $bookImage = BookImage::where('book_id', $book->id)->orderBy('id', 'desc')->first();

                $letter_photo = new LetterPhoto;
                $letter_photo->letter_id = $letter->id;
                //* uuid could not be unique() for letter_photo
                $letter_photo->uuid = $bookImage->uuid;
                $letter_photo->cloud = $bookImage->cloud;
                $letter_photo->bucket = $bookImage->bucket;
                $letter_photo->save();

                break;

            case "ap":
                //ap: accept by predecessor;
                if (is_null($ongoing_appointment)) {
                    return response()->json([
                        'code' => 4060,
                        'msg' => 'there is no ongoing appointment'
                    ]);
                }

                if ($book->holder_id != $me->id) {
                    return response()->json([
                        'code' => 4060,
                        'msg' => 'me is not the holder,predecessor.'
                    ]);
                }

                if ($ongoing_appointment->predecessor_id != $me->id) {
                    return response()->json([
                        'code' => 4060,
                        'msg' => 'me is not the appointment predecessor'
                    ]);
                }

                $ongoing_appointment->status = "ap";
                $ongoing_appointment->save();

                //todo to send sms to the successor

                break;

            case "fs":
                //fs: finished by successor receving the book.
                if (is_null($ongoing_appointment)) {
                    return response()->json([
                        'code' => 4060,
                        'msg' => 'there is no ongoing appointment'
                    ]);
                }

                if ($ongoing_appointment->successor_id != $me->id) {
                    return response()->json([
                        'code' => 4060,
                        'msg' => 'me is not the appointment successor'
                    ]);
                }

                $ongoing_appointment->status = "fs";
                //$ongoing_appointment->predecessor_comment = $request->input("comment");
                $ongoing_appointment->save();

                //
                $book->holder_id = $ongoing_appointment->successor_id;
                $book->save();

                //todo: the predecessor and the book giver get the stones

                break;

            default:
                return response()->json([
                    'code' => 4060,
                    'msg' => "unknown action."
                ]);
                break;
        }


        $msg = "success!";

        return response()->json([
            'code' => 2000,
            'msg' => $msg
        ]);

    }



//
//
//
//
//    /**
//     * @param Request $request
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function makeAppointment(Request $request) {
//
//        $this->validate($request, [
//            'book_uuid' => 'required',
//        ]);
//
//        $book_uuid = $request->input('book_uuid');
//
//        $book = Book::where('uuid', $book_uuid)->first();
//
//        if (is_null($book)) {
//
//            return response()->json([
//                'code' => 4060,
//                'msg' => 'wrong book uuid!'
//            ]);
//
//        }
//
//        $me = $request->user();
//
//        if ($book->holder_id == $me->id) {
//            return response()->json([
//                'code' => 4060,
//                'msg' => 'you are the book holder now!'
//            ]);
//        }
//
//        //current ongoing appointment
//        $ongoing_appointment = Appointment::where([
//            ['book_id', $book->id],
//            ['status', '<>', 'fb']
//        ])->first();
//
//        if (is_null($ongoing_appointment)) {
//            $stones = 1;
//        } else {
//            if ($ongoing_appointment->status == 'os') {
//                $stones = $ongoing_appointment->stones + 1;
//            } else {
//                //status == ap, fs, fp
//                return response()->json([
//                    'code' => 4060,
//                    'msg' => 'there is ongoing appointment.'
//                ]);
//            }
//        }
//
//        $appointment = new Appointment;
//        $appointment->book_id = $book->id;
//        $appointment->predecessor_id = $book->holder_id;
//        $appointment->successor_id = $me->id;
//        $appointment->stones = $stones;
//        $appointment->status = "os";
//        $appointment->save();
//
//        //todo:??? record the user's sontes. ???????
//
//        //todo: reback stones to the previous appointment sender and give one more stone to book giver and book holder.
//
//        //todo: send sms message to the book holder
//
//
//        if (!is_null($ongoing_appointment)) {
//            //ro: replaced by other user who paid more.
//            $ongoing_appointment->status = "ro";
//            $ongoing_appointment->save();
//            $ongoing_appointment->delete();
//
//            //todo give back the previous_appointment sender's stones.
//            //todo: send sms message to the previous_appointment sender.
//        }
//
//        return response()->json([
//            'code' => 2000,
//            'msg' => "success!"
//        ]);
//
//    }

//
//    /**
//     * the sender to recall the appointment
//     * @param Request $request
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function cancelAppointment(Request $request) {
//
//        $this->validate($request, [
//            'book_uuid' => 'required'
//        ]);
//
//        $book_uuid = $request->input('book_uuid');
//        $book = Book::where('uuid', $book_uuid)->first();
//
//        if (is_null($book)) {
//
//            return response()->json([
//                'code' => 4060,
//                'msg' => 'wrong book uuid.'
//            ]);
//        }
//
//        $me = $request->user();
//
//        $appointment = Appointment::where([
//            ['book_id', $book->id],
//            ['sender_id', $me->id],
//            ['status', 'os']
//        ])->first();
//
//        if (is_null($appointment)) {
//
//            return response()->json([
//                'code' => 4060,
//                'msg' => 'wrong appointment uuid.'
//            ]);
//        }
//
//        $appointment->status = 'cs';
//        $appointment->save();
//        $appointment->delete();
//
//        //todo: give back the sender's stones
//
//        return response()->json([
//            'code' => 2000,
//            'msg' => 'success!'
//        ]);
//
//    }

//
//
//    /**
//     * the receiver to accept the appointment
//     * @param Request $request
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function acceptAppointment(Request $request) {
//
//        $this->validate($request, [
//            'book_uuid' => 'required',
//            'appointment_id' => 'required|integer'
//        ]);
//
//
//        $book_uuid = $request->input('book_uuid');
//        $appointment_id = $request->input('appointment_id');
//
//        $book = Book::where('uuid', $book_uuid)->first();
//
//        if (is_null($book)) {
//
//            return response()->json([
//                'code' => 4060,
//                'msg' => 'wrong book uuid!'
//            ]);
//
//        } elseif ($book->candidate_id > 0) {
//
//            return response()->json([
//                'code' => 4060,
//                'msg' => 'the book has been reserved!'
//            ]);
//        }
//
//        $me = $request->user();
//
//        if ($book->holder_id != $me->id) {
//            return response()->json([
//                'code' => 4060,
//                'msg' => 'you are not the book holder now!'
//            ]);
//        }
//
//        $appointment = Appointment::find($appointment_id);
//
//        if ($appointment->book_id != $book->id) {
//
//            return response()->json([
//                'code' => 4060,
//                'msg' => 'appointment-book is not the book.'
//            ]);
//
//        } elseif ($appointment->receiver_id != $me->id) {
//
//            return response()->json([
//                'code' => 4060,
//                'msg' => 'appointment receiver is not the book holder.'
//            ]);
//
//        }
//
//        Appointment::where([
//            //['id', '<>', $appointment->id],
//            ['book_id', $book->id],
//            ['receiver_id', $me->id]
//        ])->delete();
//
//        $book->candidate_id = $appointment->sender_id;
//        $book->save();
//
//        return response()->json([
//            'code' => 2000,
//            'msg' => 'success!'
//        ]);
//
//    }



}
